package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tan.model.domain.User;
import com.tan.model.vo.HuanXinUserVo;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.Userapi;
import com.tanhua.server.interceptor.Userthread;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    @Autowired
    SmsTemplate smsTemplate;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    UserFreezeService userFreezeService;
    @DubboReference
    private Userapi userApi;



    public void sendMsg(String phone) {
        //根据手机号查询用户如果存在则不允许注册，如果存就判断是否被冻结
      User user =  userApi.findByMobile(phone);
        String key= Constants.USER_FREEZE+user.getId();
      String value =  redisTemplate.opsForValue().get(key);
      if(value!=null){
          Map map = JSON.parseObject(value, Map.class);
          String freezingRange = (String) map.get("freezingRange");
          userFreezeService.freeze(freezingRange,user.getId());
      }

           //1.随机生成六位数字
//       String code = (int)((Math.random()*9+1)*100000)+"";
        String code = "123456";
       //2.将验证码存入redis
         redisTemplate.opsForValue().set("smsCode_"+phone,code,100*60L, TimeUnit.SECONDS);
        //3.发送短信
//        smsTemplate.sendMsg(phone,code);
    }

    public void changePhone(String phone) {
       Long userId = Userthread.getUser().getId();
        userApi.changePhone(phone,userId);
    }


}
